(function(Jml){
	var Layouts = Jml.ElementLayout = $.IFactory({
		type : 'object',
		proto:{
			build : function(layoutType){
				var layout = this.get(layoutType),args;
				if(layout){
					return new layout(arguments[1],arguments[2]);
				}
			}
		},
		mapping:{
		//初始化布局
		init:function(op,builder){
			this.itemCount = 0;
			this.renderType = op.renderType;
			this.builder = builder;
		},
		//Tmpl_LayoutStart : null,
		//Tmpl_LayoutEnd : null,
		//Tmpl_PartStart : null,
		//Tmpl_PartEnd : null,
		//Tmpl_ItemStart : null,
		//Tmpl_ItemEnd : null,
		//布局开始方法
		//设置区域绑定属性
		setLayoutOp:function(op){
			return op;
		},
		start : function(op){
			this._layoutEnd = false;
			this.builder.pushHtml(this.Tmpl_LayoutStart,this.setLayoutOp(op));
		},
		//布局结束方法
		end : function(op){
			if(!this._layoutEnd){
				this.endPart();
				this.builder.pushHtml(this.Tmpl_LayoutEnd,op);
				this._layoutEnd = true;
			}
		},
		//检查模板项区域方法
		checkPartStart : function(item){
			return true;
		},
		//检查模板项区域结束方法
		checkPartEnd : function(item){
			return true;
		},
		//添加区域开始模板
		startPart:function(item){
			if(this.checkPartStart(item))
			{
				this._inPart && this.endPart(item);
				this._inPart = true;
				this.builder.pushHtml(this.Tmpl_PartStart,this.setPartOp(item));
			}
		},
		//添加区域结束模板
		endPart:function(item){
			if(this._inPart && this.checkPartEnd(item))
			{
				this._inPart = null;
				this.builder.pushHtml(this.Tmpl_PartEnd,item);
			}
		},
		//设置区域绑定属性
		setPartOp:function(item){
			return item;
		},
		//设置模板项绑定属性
		setItemOp : function(item){
			return item;
		},
		startItem : function(item){
			this.startPart(item);
			this.builder.pushHtml(this.Tmpl_ItemStart,this.setItemOp(item));
		},
		endItem : function(item){
			this.builder.pushHtml(this.Tmpl_ItemEnd,item);
			//this.endPart(item);
			this.itemCount++;
		}
	}
	});
	
	Layouts.add("form-Flow",{
		Tmpl_LayoutStart : '<div class="ccay-form-content">',
		Tmpl_LayoutEnd : '</div>',
		Tmpl_PartStart : '<div class="${css}"><ul>',
		Tmpl_PartEnd : '</ul></div>',
		Tmpl_ItemStart : '<li class="layout-item ccay-form-row ccay-form-${layout}"><samp class="layout-title ${_titleMark}">${name}</samp><span class="ccay-form-input">',
		Tmpl_ItemEnd : '</span></li>',
		setPartOp:function(item){
			var isCustom = item.layout == "custom";
			//判断是否为自适应区域
			if(isCustom)
			{
				css = "ccay-form-custom";
				(this.itemCount == 0) && (css += " ccay-form-custommore");
			}
			else
			{
				css = "ccay-form-body";
				this.itemCount > 0  && (css += " ccay-form-bodymore");
			}
			this._prevLayout = item.layout;
			return {css : css};
		},
		setItemOp:function(item){
			//处理必填的样式
			if(this.renderType && this.renderType !== 'view')
			{
				Ccay.Modal.isRequiredField(item) && (item._titleMark = 'required');
			}
			if(item.name){
				item.name = $i18n(item.name);
			}
			return item;
		},
		checkPartStart:function(item){
			if(this.itemCount)
			{
				//判断当前或者上一个是否为自适应区域，进行闭合操作
				if((item.layout == "custom") != (this._prevLayout == "custom"))
				{
					return true;
				}
			}
			else
				return true;
		}
	});
	
	Layouts.add("form-Table",{
		Tmpl_LayoutStart : '<table class="ccay-table">',
		Tmpl_LayoutEnd : '</table>',
		Tmpl_PartStart : '<tr>',
		Tmpl_PartEnd : '</tr>',
		Tmpl_ItemStart : '<th width="20%" class="${_titleMark}">${name}</th><td width="30%" ${_itemColspan}>',
		Tmpl_ItemEnd : '</td>',
		setItemOp:function(item){
			//处理必填的样式
			if(this.renderType && this.renderType != 'view')
			{
				Ccay.Modal.isRequiredField(item) && (item._titleMark = 'required');
			}
			if(item.layout)
			{
				item._itemColspan = "colspan='3'";
				this.rowItemCount = 2;
			}else{
				item._itemColspan = "__itemColspan";
			}
			return item;
		},
		checkPartStart:function(item){
			if(this._layoutEnd){
				this.start({});
			}
			if(this.rowItemCount > 1 ||  item.layout){
				this.builder.array[this.currItemIndex] = this.setColSpan(this.builder.array[this.currItemIndex]);
				this.rowItemCount = 0;
				return true;
			}else{
				this.currItemIndex && (this.builder.array[this.currItemIndex] = this.setColSpan(this.builder.array[this.currItemIndex], true));
			}
		},
		setColSpan : function(str, rep){
			return str.replace("__itemColspan", (this.rowItemCount > 1 || rep)  ? "" : " colspan='3'");
		},
		init:function(op,builder){
			this.rowItemCount = 0;
			this.base.init.call(this,op,builder);
			
		},
		endPart : function(item){
			this.base.endPart.call(this,item);
			if(item && item.itemType =="toolbar"){
				this.builder.array[this.currItemIndex] = this.setColSpan(this.builder.array[this.currItemIndex]);
				this.end();
			}
			
		},
		startItem : function(item){
			this.base.startItem.call(this,item);
			this.currItemIndex = this.builder.array.length - 1;
		},
		endItem : function(item){
			
			this.rowItemCount++;
			this.base.endItem.call(this,item);
		}
		
	});
})(Ccay.Jml);